package com.cheng.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.Condition;
import com.cheng.entity.MsgResponse;
import com.cheng.entity.Tieba;
import com.cheng.service.TiebaService;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * Author: Cheng
 * Date: 2019/7/22
 * Time: 14:39
 * Description: No Description
 */
public class TiebaUtil {
    @Autowired
    private TiebaSign tiebaSign;
    @Autowired
    private TiebaService tiebaService;
    private PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

    /**
     * 验证输入参数是否完整
     * @param tieba
     * @return
     */
    public static boolean checkFields(Tieba tieba) {
        return !StringUtils.isBlank(tieba.getBduss()) && !StringUtils.isBlank(tieba.getQq()) && !StringUtils.isBlank(tieba.getTaskname());
    }

    /**
     * 旧签到
     * 即时获取不将贴吧信息存数据库
     * @param tieba
     * @return
     * @throws Exception
     */
/*    public MsgRespose tbsSign(Tieba tieba) throws Exception {
        MsgRespose msgRespose = new MsgRespose();
        String bduss = tieba.getBduss();
        cm.setMaxTotal(200);             //    设置最大连接数
        cm.setDefaultMaxPerRoute(10);    //    设置每个主机的并发数
        String tbstr = tiebaSign.verification(bduss);
        if (tbstr == null) {
            tieba.setValidity(0);
            System.out.println("验证失败更新字段:" + tiebaService.update(tieba, Condition.create().eq("bduss", bduss)));
            return msgRespose.setCode(1001).setMsg("bduss验证不通过!");
        }
        JSONObject jo = (JSONObject) JSON.parse(tiebaSign.getForums(bduss, cm));
        JSONObject forum_list = jo.getJSONObject("forum_list");
        JSONArray jsonArray = forum_list.getJSONArray("non-gconforum");
        System.out.println("获取列表成功>>>>>");
        int count = 0;
        int point = 0;
        JSONArray forumArray = new JSONArray();
        for (Object o : jsonArray) {
            JSONObject forum = (JSONObject) JSON.toJSON(o);
            String id = forum.getString("id");
            String name = forum.getString("name");
            System.out.println("正在签到" + name + "：");
            String signResult = tiebaSign.signForums(name, id, tbstr, bduss, cm);
            JSONObject result = (JSONObject) JSON.parse(signResult);
            if (signResult.contains("error_code") && signResult.contains("error_msg")) {
                System.out.println(result.getString("error_msg"));
                continue;
            }
            JSONObject user_info = result.getJSONObject("user_info");
            point += Integer.valueOf(user_info.getString("sign_bonus_point"));
            System.out.println("签到成功!经验+" + user_info.getString("sign_bonus_point") + ",今天第" + user_info.getString("user_sign_rank") + "个签到");
            count++;
        }
        tieba.setAmount(count+"/"+forumArray.size());
        tieba.setPoints(point);
        tieba.setVisitTime(new Date());
        System.out.println("======" + tieba);
        tieba.setVisitCount(tieba.getVisitCount() + 1);
        tieba.updateById();
        return msgRespose.setCode(1000).setMsg("今日共签到成功" + count + "个贴吧");
    }*/

}
